﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class QuanTri_TrangBangDiemMonHoc : System.Web.UI.Page
{
    SoLienLacDataContext db = new SoLienLacDataContext();
    clsLoadCombobox cls = new clsLoadCombobox();
    clsKiemTraTrangThai clstt = new clsKiemTraTrangThai();
    clsThaoTac thaotac = new clsThaoTac();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
        }
        else
        {
            
            if (!IsPostBack)
            {
                cls.LoadComboboxNamHoc(drNamHoc); 
                cls.LoadComboboxKhoi(drKhoiHoc);
                int makhoi = int.Parse(drKhoiHoc.SelectedValue.ToString());
                TaoTabLopDong(makhoi);
                int manam = int.Parse(drNamHoc.SelectedValue.ToString());
                //cls.LoadComboboxLopHoc(drLopHoc, int.Parse(drNamHoc.SelectedValue.ToString()));               
                btnDuyet.Visible = false;
                btnSuaLai.Visible = false;
                btnHuyDuyet.Visible = false;
                int maky = 1;
                if (rdoKyII.Checked == true)
                { maky = 2; }
                cls.LoadCBMon(drMonHoc);
                int malop = LayMaLop();
                //cls.LoadComboboxMonHoc(drMonHoc,malop, int.Parse(drNamHoc.SelectedValue.ToString()));
                //TaoTabLopDong(makhoi);
                int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
                KTHienThi2(manam,malop,mamon,maky);

            }
        }

    }
    void HienThiBangDiemMonHoc(int manam, int maky,int malop,int mamon)
    {
        grvNhapDiem.DataSource = db.LayDanhSachHocSinhTheoLop(mamon, maky, manam, malop);
        grvNhapDiem.DataBind();

    }
    void KTHienThi()
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = LayMaLop();
        int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoKyI.Checked == true)
        { maky = 1; }
        if (rdoKyII.Checked == true)
        { maky = 2; }

        lblTrangThai.Text = clstt.trangthai(manam, malop, mamon, maky);

        if (lblTrangThai.Text == "Chưa gửi bảng điểm")
        {
            btnDuyet.Visible = false;
            btnSuaLai.Visible = false;
            btnHuyDuyet.Visible = false;
            grvNhapDiem.Visible = false;
            lblNgayGui.Text = "";
            lblNgayDuyet.Text = "";


        }
        if (lblTrangThai.Text != "Chưa gửi bảng điểm")
        {
            grvNhapDiem.Visible = true;
            if (lblTrangThai.Text == "Đã duyệt")
            {
                clstt.NgayDuyetBD(manam, malop, mamon, maky, lblNgayDuyet);
                btnDuyet.Visible = false;
                btnSuaLai.Visible = false;
                btnHuyDuyet.Visible = true;
            }
            if (lblTrangThai.Text == "Đang chờ duyệt")
            {
                lblNgayDuyet.Text = "";
                btnDuyet.Visible = true;
                btnSuaLai.Visible = true;
                btnHuyDuyet.Visible = false;
            }
            if (lblTrangThai.Text == "Cần sửa lại bảng điểm")
            {
                lblNgayDuyet.Text = "";
                btnDuyet.Visible = false;
                btnSuaLai.Visible = false;
                btnHuyDuyet.Visible = false;
            }
            if (lblTrangThai.Text == "Đã sửa và đang chờ duyệt")
            {
                lblNgayDuyet.Text = "";
                btnDuyet.Visible = true;
                btnSuaLai.Visible = true;
                btnHuyDuyet.Visible = false;
            }
            clstt.NgayGuiBD(manam, malop, mamon, maky, lblNgayGui);
            HienThiBangDiemMonHoc(manam, maky, malop, mamon);


        }
    }

    void KTHienThi2(int manam, int malop, int mamon, int maky)
    {
        lblTrangThai.Text = clstt.trangthai(manam, malop, mamon, maky);

        if (lblTrangThai.Text == "Chưa gửi bảng điểm")
        {
            btnDuyet.Visible = false;
            btnSuaLai.Visible = false;
            btnHuyDuyet.Visible = false;
            grvNhapDiem.Visible = false;
            lblNgayGui.Text = "";
            lblNgayDuyet.Text = "";

        }
        if (lblTrangThai.Text != "Chưa gửi bảng điểm")
        {
            grvNhapDiem.Visible = true;
            if (lblTrangThai.Text == "Đã duyệt")
            {
                clstt.NgayDuyetBD(manam, malop, mamon, maky, lblNgayDuyet);
                btnDuyet.Visible = false;
                btnSuaLai.Visible = false;
                btnHuyDuyet.Visible = true;
            }
            if (lblTrangThai.Text == "Đang chờ duyệt")
            {
                lblNgayDuyet.Text = "";
                btnDuyet.Visible = true;
                btnSuaLai.Visible = true;
                btnHuyDuyet.Visible = false;
            }
            if (lblTrangThai.Text == "Cần sửa lại bảng điểm")
            {
                lblNgayDuyet.Text = "";
                btnDuyet.Visible = false;
                btnSuaLai.Visible = false;
                btnHuyDuyet.Visible = false;
            }
            if (lblTrangThai.Text == "Đã sửa và đang chờ duyệt")
            {
                lblNgayDuyet.Text = "";
                btnDuyet.Visible = true;
                btnSuaLai.Visible = true;
                btnHuyDuyet.Visible = false;
            }
            clstt.NgayGuiBD(manam, malop, mamon, maky, lblNgayGui);
            HienThiBangDiemMonHoc(manam, maky, malop, mamon);
        }
    }
    protected void btnHienThi_Click(object sender, EventArgs e)
    {
        
        

    }
   
    protected void btnDuyet_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = LayMaLop();
        int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoKyI.Checked == true)
        { maky = 1; }
        if (rdoKyII.Checked == true)
        { maky = 2; }

        if (clstt.TTHoanThanh(manam, malop, mamon, maky) == false)
        {
            TrangThaiBangDiem ttbd = db.TrangThaiBangDiems.SingleOrDefault(p => p.MaKy == maky && p.MaLop == malop && p.MaNam == manam && p.MaMon == mamon);
            ttbd.TrangThaiChoDuyet = true;
            ttbd.TrangThaiHoanThanh = true;
            ttbd.ChuaSua = false;
            ttbd.DaSua = false;
            ttbd.NgayDuyetBD = DateTime.Now.Date;
            if (txtThongBao.Text != "" || txtThongBao.Text != null)
            {
                ttbd.ThongBao = txtThongBao.Text;
            }
            db.SubmitChanges();
        }
        lblTrangThai.Text = "Đã duyệt";
        btnSuaLai.Visible = false;
        btnDuyet.Enabled = false;
        clstt.NgayDuyetBD(manam, malop, mamon, maky, lblNgayDuyet);
        string tentv = "Duyệt bảng điểm";
        string tengv = Session["Username"].ToString();
        var c1 = from p in db.Admins where p.TenDangNhap == tengv select p.MaAdmin;
        string noidung = "Đã duyệt bảng điểm lớp" + tabLop.ActiveTab.HeaderText + " " + "của môn" + " " + drMonHoc.SelectedItem.Text;
        clsCapNhatTacVu tacvu = new clsCapNhatTacVu();
        tacvu.ThemTacVuAdmin(tentv, noidung, DateTime.Now, int.Parse(c1.First().ToString()));
        
    }
    protected void btnSuaLai_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = LayMaLop();
        int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoKyI.Checked == true)
        { maky = 1; }
        if (rdoKyII.Checked == true)
        { maky = 2; }

        if (clstt.TTHoanThanh(manam, malop, mamon, maky) == false)
        {
            TrangThaiBangDiem ttbd = db.TrangThaiBangDiems.SingleOrDefault(p => p.MaKy == maky && p.MaLop == malop && p.MaNam == manam && p.MaMon == mamon);
            ttbd.TrangThaiChoDuyet = false;
            ttbd.TrangThaiHoanThanh = false;
            ttbd.ChuaSua = true;
            ttbd.DaSua = false;
            if (txtThongBao.Text != "" || txtThongBao.Text != null)
            {
                ttbd.ThongBao = txtThongBao.Text;
            }
            db.SubmitChanges();
        }
        lblTrangThai.Text = "Yêu cầu sửa lại";
        string tentv = "Yêu cầu sửa bảng điểm";
        string tengv = Session["Username"].ToString();
        var c1 = from p in db.Admins where p.TenDangNhap == tengv select p.MaAdmin;
        string noidung = "Yêu cầu giáo viên sửa bảng điểm lớp" + tabLop.ActiveTab.HeaderText + " " + "của môn" + " " + drMonHoc.SelectedItem.Text;
        clsCapNhatTacVu tacvu = new clsCapNhatTacVu();
        tacvu.ThemTacVuAdmin(tentv, noidung, DateTime.Now, int.Parse(c1.First().ToString()));
    }
    protected void btnHuyDuyet_Click(object sender, EventArgs e)
    {
        btnDuyet.Enabled = true;
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = LayMaLop();
        int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoKyI.Checked == true)
        { maky = 1; }
        if (rdoKyII.Checked == true)
        { maky = 2; }

        if (clstt.TTHoanThanh(manam, malop, mamon, maky) == true)
        {
            TrangThaiBangDiem ttbd = db.TrangThaiBangDiems.SingleOrDefault(p => p.MaKy == maky && p.MaLop == malop && p.MaNam == manam && p.MaMon == mamon);
            ttbd.TrangThaiChoDuyet = true;
            ttbd.TrangThaiHoanThanh = false;
            ttbd.ChuaSua = true;
            ttbd.DaSua = false;
            if (txtThongBao.Text != "" || txtThongBao.Text != null)
            {
                ttbd.ThongBao = txtThongBao.Text;
            }
            db.SubmitChanges();
        }
        lblTrangThai.Text = "Chưa duyệt";
        btnSuaLai.Visible = true;
       
        string tentv = "Hủy duyệt bảng điểm";
        string tengv = Session["Username"].ToString();
        var c1 = from p in db.Admins where p.TenDangNhap == tengv select p.MaAdmin;
        string noidung = "Hủy duyệt bảng điểm lớp" + tabLop.ActiveTab.HeaderText + " " + "của môn" + " " + drMonHoc.SelectedItem.Text;
        clsCapNhatTacVu tacvu = new clsCapNhatTacVu();
        tacvu.ThemTacVuAdmin(tentv, noidung, DateTime.Now, int.Parse(c1.First().ToString()));
    }
    protected void grvNhapDiem_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvNhapDiem.PageIndex = e.NewPageIndex;
        int malop = LayMaLop();
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoKyI.Checked == true)
        { maky = 1; }
        if (rdoKyII.Checked == true)
        { maky = 2; }
        this.HienThiBangDiemMonHoc(manam, maky, malop, mamon);
    }
    protected void rdoKyI_CheckedChanged(object sender, EventArgs e)
    {
        TaoTabLopDong(int.Parse(drKhoiHoc.SelectedValue.ToString()));
        int malop = LayMaLop();
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoKyI.Checked == true)
        { maky = 1; }
        if (rdoKyII.Checked == true)
        { maky = 2; }
        
        KTHienThi2(manam,malop,mamon,maky);
    }
    protected void rdoKyII_CheckedChanged(object sender, EventArgs e)
    {
        TaoTabLopDong(int.Parse(drKhoiHoc.SelectedValue.ToString()));
        int malop = LayMaLop();
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoKyI.Checked == true)
        { maky = 1; }
        if (rdoKyII.Checked == true)
        { maky = 2; }

        KTHienThi2(manam, malop, mamon, maky);
    }
    protected void drLopHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        int malop = LayMaLop();
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoKyI.Checked == true)
        { maky = 1; }
        if (rdoKyII.Checked == true)
        { maky = 2; }

        KTHienThi2(manam, malop, mamon, maky);
    }
    protected void drMonHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        TaoTabLopDong(int.Parse(drKhoiHoc.SelectedValue.ToString()));
        int malop = LayMaLop();
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoKyI.Checked == true)
        { maky = 1; }
        if (rdoKyII.Checked == true)
        { maky = 2; }
        
        KTHienThi2(manam, malop, mamon, maky);
    }
    void TaoTabLopDong(int makhoi)
    {
        tabLop.Tabs.Clear();
        var c = from p in db.LopHocs where p.MaKhoi == makhoi select new { p.MaLop, p.TenLop };
        if (c.Count() != 0)
        {
            foreach (var con in c)
            {
                //int i = 0;
                AjaxControlToolkit.TabPanel tabcon = new AjaxControlToolkit.TabPanel();
                tabcon.HeaderText = con.TenLop;
                tabcon.ID = "tab" + con.MaLop.ToString();
                
                tabLop.Tabs.Add(tabcon);
            }
        }
       // tabLop.ActiveTabIndex = 0;
    }
    protected void tabLop_ActiveTabChanged(object sender, EventArgs e)
    {
        
        //cls.LoadComboboxKhoi(drKhoiHoc);
        int makhoi = int.Parse(drKhoiHoc.SelectedValue.ToString());
        TaoTabLopDong(makhoi);
        tabLop.AutoPostBack = true;
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = LayMaLop();
        //cls.LoadComboboxMonHoc(drMonHoc, malop, int.Parse(drNamHoc.SelectedValue.ToString()));
        int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoKyI.Checked == true)
        { maky = 1; }
        if (rdoKyII.Checked == true)
        { maky = 2; }
        
        KTHienThi2(manam, malop, mamon, maky);
        

    }
    private int LayMaLop()
    {
        int malop=0;
        string ma = tabLop.ActiveTab.ID.ToString();
         malop = int.Parse(ma.Substring(3, 1));
         return malop;
    }
    protected void drKhoiHoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        TaoTabLopDong(int.Parse(drKhoiHoc.SelectedValue.ToString()));
        int malop = LayMaLop();
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoKyI.Checked == true)
        { maky = 1; }
        if (rdoKyII.Checked == true)
        { maky = 2; }
        
        KTHienThi2(manam, malop, mamon, maky);
    }
    protected void btnXuatExcel_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = LayMaLop();
        int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
        int maky = 0;
        string hocky = "";
        if (rdoKyI.Checked == true)
        { maky = 1; hocky = "I"; }
        if (rdoKyII.Checked == true)
        { maky = 2; hocky = "II"; }
        string tenlop = tabLop.ActiveTab.HeaderText;
        DataTable dt = thaotac.ToDataTable(db, db.funcLayDiemHSTheoLop(manam, maky, manam, malop));

        thaotac.Export(dt, "Môn " + drMonHoc.SelectedItem.Text, "BẢNG ĐIỂM MÔN " + drMonHoc.SelectedItem.Text + "  " + "   HỌC KỲ " + hocky + " NĂM HỌC: " + drNamHoc.SelectedItem.Text + " LỚP HỌC: " + tenlop);
    }
}
